package com.inossem.warehousestorage_inossem.adapter.allocation;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;

import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.allocation.in_storage.AllocationInBatchDetailsActivity;
import com.inossem.warehousestorage_inossem.activity.allocation.in_storage.AllocationInSNDetailsActivity;
import com.inossem.warehousestorage_inossem.activity.allocation.in_storage.extra.AllocationInStorageExtra;
import com.inossem.warehousestorage_inossem.bean.allocation.AllocationInMatsBean;
import com.inossem.warehousestorage_inossem.utils.OutStorageUtils;
import com.zhy.android.percent.support.PercentLinearLayout;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.List;

/**
 * TODO
 *
 * @author 詹建宇
 * @time on 2018/4/3 16:49
 * @email jianyu.zhan@inossem.com
 */
public class AllocationInMatsAdapter extends BaseAdapter {

    private Context mContext;
    private List<AllocationInMatsBean.MatsBodyBean.ItemListBean> list;
    private AllocationInMatsBean.MatsBodyBean matsBodyBean;

    public AllocationInMatsAdapter(Context mContext, List<AllocationInMatsBean.MatsBodyBean.ItemListBean> list, AllocationInMatsBean.MatsBodyBean bean) {
        this.mContext = mContext;
        this.list = list;
        this.matsBodyBean = bean;
    }

    @Override
    public int getCount() {
        return list.size() + 1;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.cy_view_in_storage_allocation_mats_first, null);
            TextView outPlaceTv = (TextView) convertView.findViewById(R.id.outPlaceTv);
            TextView inPlaceTv = (TextView) convertView.findViewById(R.id.inPlaceTv);
            TextView personTv = (TextView) convertView.findViewById(R.id.personTv);
            TextView timeTv = (TextView) convertView.findViewById(R.id.timeTv);
            EditText editText = (EditText) convertView.findViewById(R.id.remarkTv);
            PercentLinearLayout hideView = (PercentLinearLayout) convertView.findViewById(R.id.hideView);
            TextView showAndHideTv = (TextView) convertView.findViewById(R.id.showAndHideTv);

            outPlaceTv.setText(String.valueOf(matsBodyBean.getLocation_input_name()));
            inPlaceTv.setText(String.valueOf(matsBodyBean.getLocation_output_name()));
            personTv.setText(String.valueOf(matsBodyBean.getApplicant()));
            timeTv.setText(String.valueOf(matsBodyBean.getDemand_date()));

            hideView.setVisibility(View.GONE);
            showAndHideTv.setText("展开详情");

            showAndHideTv.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    switch (hideView.getVisibility()) {
                        case View.VISIBLE:
                            hideView.setVisibility(View.GONE);
                            showAndHideTv.setText("展开详情");
                            break;
                        case View.GONE:
                            hideView.setVisibility(View.VISIBLE);
                            showAndHideTv.setText("收起详情");
                            break;
                    }
                }
            });

        } else {
            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.view_out_storage_picking_item_list, null);
                viewHolder = new ViewHolder();
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            AllocationInMatsBean.MatsBodyBean.ItemListBean bean = list.get(position - 1);
            initData(mContext, viewHolder, convertView, bean);
        }
        return convertView;
    }

    private class ViewHolder {
        private TextView materielCode, materielDescription, outStorageNumber, outStorageNumberTextView;
        private PercentRelativeLayout headLayout;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.materielCode = (TextView) view.findViewById(R.id.materielCode);
        viewHolder.materielDescription = (TextView) view.findViewById(R.id.materielDescription);
        viewHolder.outStorageNumber = (TextView) view.findViewById(R.id.outStorageNumber);
        viewHolder.headLayout = (PercentRelativeLayout) view.findViewById(R.id.headLayout);
        viewHolder.outStorageNumberTextView = (TextView) view.findViewById(R.id.outStorageNumberTextView);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, AllocationInMatsBean.MatsBodyBean.ItemListBean bean) {

        viewHolder.outStorageNumberTextView.setText("入库数量");
        viewHolder.materielCode.setText(bean.getMat_code());
        viewHolder.materielDescription.setText(bean.getMat_name());

        viewHolder.outStorageNumber.setText(TextUtils.isEmpty(bean.getInNum()) ? mContext.getResources().getString(R.string.activity_out_storage_sale_overview_real_hair)
                : (mContext.getResources().getString(R.string.activity_out_storage_sale_overview_fill_in_the_number) + bean.getInNum() + bean.getName_zh()));

        if (String.valueOf(bean.getIsOk()).equals("1")) {
            viewHolder.headLayout.setBackgroundResource(R.color.test_green);
        } else {
            viewHolder.headLayout.setBackgroundResource(R.color.view_receive_item_list_dark_row);
        }

        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent;
                switch (bean.getMat_store_type()) {
                    case OutStorageUtils.TYPE_BATCH:
                    case OutStorageUtils.TYPE_NONE:
                        //批次 类别
                        intent = new Intent(context, AllocationInBatchDetailsActivity.class);
                        intent.putExtra(AllocationInStorageExtra.MAT_ID, bean.getMat_id());
                        context.startActivity(intent);
                        break;
                    case OutStorageUtils.TYPE_SN:
                        //序列号 类别
                        intent = new Intent(context, AllocationInSNDetailsActivity.class);
                        intent.putExtra(AllocationInStorageExtra.MAT_ID, bean.getMat_id());
                        context.startActivity(intent);
                        break;
                }
            }
        });
    }
}
